<template>
  <div class="app-container">
    <h1> 供应商管理</h1>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="供应商名称" prop="supplierFullName">
        <el-input
          v-model="queryParams.supplierFullName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button

          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['erp_supplier:management:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['erp_supplier:management:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['erp_supplier:management:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['erp_supplier:management:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商ID，主键" align="center" prop="id" />
      <el-table-column label="供应商编码" align="center" prop="supplierCode" />
      <el-table-column label="供应商简称" align="center" prop="supplierShortName" />
      <el-table-column label="供应商名称" align="center" prop="supplierFullName" />
      <el-table-column label="付款方式" align="center" prop="paymentMethod" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="供应商电话" align="center" prop="supplierPhone" />
      <el-table-column label="交易币种" align="center" prop="transactionCurrency" />
      <el-table-column label="送货方式" align="center" prop="deliveryMethod" />
      <el-table-column label="上传状态" align="center" prop="uploadStatus" >


        <template slot-scope="{ row }">
        {{ row.uploadStatus ===0 ? '未上传' : row.uploadStatus ===1 ? '已上传' : '未知' }}
      </template>
        </el-table-column >
      <el-table-column label="审核状态" align="center" prop="audit"   :formatter="formatAuditStatus" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['erp_supplier:management:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['erp_supplier:management:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

<!--     添加或修改供应商管理对话框-->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="供应商编码" prop="supplierCode" label-width="150">
          <el-input v-model="form.supplierCode" placeholder="请输入供应商编码" />
        </el-form-item>
        <el-form-item label="供应商简称" prop="supplierShortName" label-width="150">
          <el-input v-model="form.supplierShortName" placeholder="请输入供应商简称" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierFullName" label-width="150">
          <el-input v-model="form.supplierFullName" placeholder="请输入供应商名称" />
        </el-form-item>

        <el-form-item label="联系人" prop="contactPerson"  label-width="150" >
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email" label-width="150">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="供应商电话" prop="supplierPhone" label-width="150">
          <el-input v-model="form.supplierPhone" placeholder="请输入供应商电话" />
        </el-form-item>
        <el-form-item label="交易币种" prop="transactionCurrency" label-width="150">
          <el-input v-model="form.transactionCurrency" placeholder="请输入交易币种" />
        </el-form-item>


      </el-form>
      <div  slot="footer" class="dialog-footer" label-width="150">
        <el-button type="primary"   @click="saveForm" :class="{ 'hidden': baocun }" :disabled="baocun">保存</el-button>
        <el-button type="primary" v-if="isEditMode" @click="auditForm">审核并新增</el-button>
        <el-button type="primary" :class="{ 'hidden': shenhe }"v-else @click="showApprovalDialog" :disabled="shenhe">仅审核</el-button>

        <el-button type="primary" @click="tijiao"  v-if="isEditMode"  >提交并新增</el-button>
        <el-button type="primary" :class="{ 'hidden': tijiaoaudit }"v-else @click="updatetijiao"   :disabled="tijiaoaudit">仅提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>



    </el-dialog>


    <el-dialog
      title="审批"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">

      <el-form  label-width="100px" :model="form2">
        <el-form-item label="审批意见">
          <el-input
            type="textarea"
            :rows="3"
            placeholder="请输入审批意见"
            v-model="form2.opinion">
          </el-input>
        </el-form-item>

        <el-form-item label="审批结果" >
          <el-radio-group v-model="choices">
            <el-radio label="agree">同意</el-radio>
            <el-radio label="reject">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateshenhe">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listManagement,
  getManagement,
  delManagement,
  addManagement,
  updateManagement,
  baocun, shenhe, tijiao, updatetijiao, updateshenhe
} from "@/api/erp_supplier/management";

export default {
  name: "Management",
  data() {
    return {
      uid:'',
      tijiaoaudit:true,
      shenhe:true,
      baocun:false,
      form2:{},
      dialogVisible: false,//审核弹出框 同意或拒绝
      isDisabled: false, // 设置为true禁用按钮      // 遮罩层
      isEditMode: true,
      choices:'',
      choice:'', //选中 0拒绝 1同意
      opinion:'', //审核意见
      // 选中数组


      loading: true,

      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商管理表格数据
      managementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierFullName: null,
      },
      // 表单参数
      form: {
        audit:''
      },
      // 表单校验
      rules: {
        supplierCode: [
          { required: true, message: "供应商编码不能为空", trigger: "blur" }
        ],
        supplierShortName: [
          { required: true, message: "供应商简称不能为空", trigger: "blur" }
        ],
        supplierFullName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    showApprovalDialog() {
      this.dialogVisible = true
    },


    updatetijiao() {
      console.log(this.uid);
      updatetijiao(this.uid).then(response => {
        this.form = response.data;
      });
    },
    updateshenhe() {
      console.log(this.uid);
      console.log(this.form2.opinion);
      if (this.form2.opinion===''||this.form2.opinion==null) {
        this.$message.warning('请输入审批意见')
        return
      }
      // 提交审批逻辑
      console.log('提交审批:', this.opinion)
      // 根据结果执行不同操作
      if (this.choices === 'agree') {
        this.choice=1
      } else {
        this.choice=0
      }
      // 关闭弹窗
      this.dialogVisible = false
      // 重置表单
      this.resetForm()
      console.log(this.choice);
      updateshenhe(this.uid,this.form2.opinion,this.choice).then(response => {
      }).catch(error => {
        console.log(error.response.data.msg); // 失败时："审核失败"
      });
    },
    handleClose(done) {
      // 关闭前的回调
      this.$confirm('确定要关闭审批窗口吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        done()
        this.resetForm()
      }).catch(() => {})
    },
    resetForm() {
      this.approvalForm = {
        opinion: '',
        result: 'agree'
      }
    },

    saveForm() {
      this.form.audit=0
      console.log('保存表单:', this.form);
      baocun(this.form).then(response => {
        this.managementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.$message.success('保存成功');
    },

    auditForm() {
      this.form.audit=2
      console.log('审核表单:', this.form);
      shenhe(this.form).then(response => {
        this.managementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.$message.info('已提交审核');
    },
    tijiao() {
      this.form.audit=1
      console.log('提交表单:', this.form);
      tijiao(this.form).then(response => {
        this.managementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.
      this.$message.success('审核成功');
    },
    cancelForm() {
      this.$confirm('确定要取消吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.resetForm();
        this.$message.info('已取消');
      });
    },

    formatAuditStatus(row, column, cellValue) {
      const statusMap = {
        0: '草稿',
        1: '待审核',
        2: '已审核',
        3: '被驳回',
        4: '审核完成',
        5: '强制终止'
      }
      return statusMap[cellValue] || '未知状态'
    },

    /** 查询供应商管理列表 */
    getList() {
      this.loading = true;
      listManagement(this.queryParams).then(response => {
        this.managementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supplierCode: null,
        supplierShortName: null,
        supplierFullName: null,
        paymentMethod: null,
        contactPerson: null,
        email: null,
        supplierPhone: null,
        transactionCurrency: null,
        deliveryMethod: null,
        uploadStatus: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isEditMode = true;
      this.reset();
      this.open = true;
      this.title = "添加供应商管理";
    },
    /** 修改按钮操作 */
      handleUpdate(row) {
      this.isEditMode=false
      this.baocun=true
      this.reset();
      const id = row.id || this.ids
      this.uid=id;
      getManagement(id).then(response => {
        this.form = response.data;
        console.log(this.form);
        //保存判断
        // if (this.form.audit===1||this.form.audit===2||this.form.audit===3){
        //   this.baocun = true
        // }
        //审核判断
        if (this.form.audit===1){
          this.shenhe=false
          this.tijiaoaudit=true;

        }
        if (this.form.audit===2){
          this.tijiaoaudit=true;

        }
        //提交
        if(this.form.audit===0){
          this.tijiaoaudit=false;
        }
        this.open = true;
        this.title = "修改供应商管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateManagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除供应商管理编号为"' + ids + '"的数据项？').then(function() {
        return delManagement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('erp_supplier/management/export', {
        ...this.queryParams
      }, `management_${new Date().getTime()}.xlsx`)
    }
  }};
</script>
<style>
.hidden {
  display: none;
}
</style>
